package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;

    /**
     * 支付，生成订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 分页查询订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R  page(int page,int pageSize){
        //构造dto
        //复制两个集合
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> query = new LambdaQueryWrapper<>();
        query.orderByDesc(Orders::getOrderTime);
        Page<OrdersDto> ordersDtoPage = getOrdersDtoPage(ordersPage, query);
        return R.success(ordersDtoPage);
    }

    private Page<OrdersDto> getOrdersDtoPage(Page<Orders> ordersPage, LambdaQueryWrapper<Orders> query) {
        Page<Orders> ordersPage1 = orderService.page(ordersPage, query);
        List<OrdersDto> records = ordersPage1.getRecords().stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            //地址id查姓名
            AddressBook byId = addressBookService.getById(item.getAddressBookId());
            ordersDto.setConsignee(byId.getConsignee());
            ordersDto.setUserName(byId.getConsignee());
            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderDetail::getOrderId, item.getNumber());
            List<OrderDetail> orderDetailList = orderDetailService.list(wrapper);
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());
        //二次复制
        Page<OrdersDto> ordersDtoPage = new Page<>();
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");
        ordersDtoPage.setRecords(records);
        return ordersDtoPage;
    }

    //订单明细展示
    @GetMapping("/page")
    public R<Page<OrdersDto>> getOrder(int page, int pageSize, Integer number, String beginTime, String endTime) {
        log.info("获取到的订单信息:" + page + " " + pageSize + " " + number + " " + beginTime + " " + endTime);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> query = new LambdaQueryWrapper<>();
        query.orderByDesc(Orders::getOrderTime);
        query.like(number != null, Orders::getNumber, number);
        if (beginTime != null && endTime != null) {
            query.between(Orders::getOrderTime, beginTime, endTime);
        }
        Page<OrdersDto> ordersDtoPage = getOrdersDtoPage(ordersPage, query);
        return R.success(ordersDtoPage);
    }

    //派送
    @PutMapping
    public R<String> putStatus(@RequestBody Orders orders) {
        LambdaUpdateWrapper<Orders> update = new LambdaUpdateWrapper<>();
        update.set(Orders::getStatus, orders.getStatus());
        update.eq(Orders::getId, orders.getId());
        orderService.update(update);
        return R.success("更改成功");
    }

    //再来一单
    @PostMapping("/again")
    public R<String> orderAgain(@RequestBody Orders orders) {
        Long orderId = orders.getId();
        //获取订单根据id
        Orders orderServiceById = orderService.getById(orderId);
        //根据id查询订单明细
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<OrderDetail>();
        wrapper.eq(OrderDetail::getOrderId, orderId);
        List<OrderDetail> orderDetailList = orderDetailService.list(wrapper);
        //将信息添加进购物车里
        for (OrderDetail orderDetail : orderDetailList) {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setId(IdWorker.getId());
            shoppingCart.setName(orderDetail.getName());
            shoppingCart.setUserId(orderServiceById.getUserId());
            shoppingCart.setDishId(orderDetail.getDishId());
            shoppingCart.setSetmealId(orderDetail.getSetmealId());
            shoppingCart.setDishFlavor(orderDetail.getDishFlavor());
            shoppingCart.setNumber(orderDetail.getNumber());
            shoppingCart.setAmount(orderDetail.getAmount());
            shoppingCart.setImage(orderDetail.getImage());
            shoppingCart.setCreateTime(LocalDateTime.now());
            //装入数据库
            shoppingCartService.save(shoppingCart);
        }

        return R.success("再来一单成功");
    }
}
